package com.ruoyi.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.entity.system.SysUser;
import com.ruoyi.common.enums.ExceptionEnum;
import com.ruoyi.common.util.Result;
import com.ruoyi.common.util.page.PageUtils;
import com.ruoyi.common.util.security.SecurityUtils;
import com.ruoyi.quartz.entity.SysJob;
import com.ruoyi.quartz.service.ISysJobService;
import com.ruoyi.system.service.ISysUserService;
import lombok.extern.java.Log;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author LiuXiaoYu
 * @date 2021/10/12- 12:14
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysJobService sysJobService;


    @GetMapping("/list")
    @PreAuthorize("@ruoyi.hasPermi('system:user:list')")
    public Result<?> list(SysUser sysUser){
        //在这里设置页数
        Page<SysUser> page = PageUtils.buildPage();
        PageUtils userList = sysUserService.getUserList(sysUser,page);
        Page<SysJob> page2 = PageUtils.buildPage();
        List<SysJob> sysJobs=sysJobService.getJobList(page2);
        sysJobs.forEach(System.out::println);
        return Result.success(userList);
    }

    /**
     * 新增用户
     * @param sysUser
     * @return
     */
    @PreAuthorize("@ruoyi.hasPermi('system:user:add')")
    @PostMapping("/add")
    public Result<?> add(@RequestBody @Validated SysUser sysUser){
        sysUser.setPassword(SecurityUtils.encryptPassword(sysUser.getPassword()));
        int row = sysUserService.insertUser(sysUser);
        if (row>0){
            return Result.success();
        }
        return Result.error(ExceptionEnum.ERROR,"新增用户失败");
    }

}
